<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/system/barcode/barcodeList' }">规则列表</el-breadcrumb-item>
      <el-breadcrumb-item>详情信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <div class="autumn-center">
      <div class="edit-form">
        <el-form ref="editFormRef" size="large" label-width="90px" :model="editForm" :rules="editFormRules">
          <el-row>
            <el-col :span="12">
              <el-form-item label="类型" prop="type">
                <el-select v-model="editForm.type" placeholder="请选择" @change="changeType">
                  <el-option v-for="(value, key, index) in $listDict('BARCODE_RULE')" :key="index" :label="value"
                    :value="key"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="值" prop="value">
                <el-select v-if="editForm.type == '2' || editForm.type == '3'" v-model="editForm.value"
                  placeholder="请选择">
                  <el-option
                    v-for="(value, key, index) in $listDict(editForm.type == '3' ? 'DATE_FORMAT' : 'SEQUENCE_RESET')"
                    :key="index" :label="value" :value="key"></el-option>
                </el-select>

                <el-input v-else v-model="editForm.value" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序" prop="sort">
                <el-input-number v-model="editForm.sort" :min="1" />
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="editForm.type == '2'">
              <el-form-item label="长度" prop="extent">
                <el-input v-model="editForm.extent" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="分隔符" prop="delimit">
                <el-input v-model="editForm.delimit" />
              </el-form-item>
            </el-col>
            <el-col :span="editForm.type != '2' ? 24 : 12">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" resize="none" v-model="editForm.remark" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <div class="form-foot-button">
          <el-button type="primary" @click="submit(editFormRef)">保存</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { FormInstance } from 'element-plus'
import { viewBarcodeRule, saveBarcodeRule, updateBarcodeRule } from 'src/api/system/barcodeRule'
import { $listDict } from 'src/hooks/useCommon'

const router = useRouter()
const editFormRef = ref<FormInstance>()

// 定义表单数据
const editForm = reactive({
  id: '',
  barcodeId: '',
  type: '',
  value: '',
  sort: 10,
  extent: '',
  delimit: '',
  remark: ''
})
const editFormRules = {
  type: [
    { required: true, message: '请输入规则类型', trigger: ['blur', 'change'] }
  ],
  value: [
    { required: true, message: '请输入规则值', trigger: ['blur', 'change'] }
  ],
  sort: [
    { required: true, message: '请输入排序', trigger: 'blur' }
  ],
  extent: [
    { required: true, message: '请输入长度', trigger: 'blur' },
    { pattern: /^[1-9]\d*$/, message: '请输入正整数', trigger: ['change', 'blur'] }
  ]
}

// 修改时填充表单数据
const route = useRoute()
if (route.query.id) {
  const id = route.query.id as string
  viewBarcodeRule(id).then(({ data }) => {
    Object.assign(editForm, data)
  })
} else {
  editForm.barcodeId = route.query.barcodeId as string
}

// 更改规则
const changeType = (value: string) => {
  editForm.value = ''
  editForm.sort = 10
  editForm.extent = ''
  editForm.delimit = ''
  editForm.remark = ''
  editForm.value = ''
}

// 提交
const submit = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate(valid => {
    if (!valid) return
    if (!editForm.id) {
      saveBarcodeRule(editForm).then(() => {
        cancel()
      })
    } else {
      updateBarcodeRule(editForm).then(() => {
        cancel()
      })
    }
  })
}

const cancel = () => {
  router.push({
    path: '/system/barcodeRule/barcodeRuleList',
    query: { barcodeId: editForm.barcodeId }
  })
}
</script>

<style lang="scss" scoped>
</style>
